<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 组织架构-头部 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 组织架构-内容 -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- scope-scope是 tree组件传给每个节点的插槽的内容的数据 -->
          <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>
      <!-- 添加部门弹层 -->
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils/index'
import addDept from './components/add-dept.vue'

export default {
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      company: { },
      departs: [],
      defaultProps: {
        label: 'name' // 显示的节点名字(默认显示label)
      },
      showDialog: false, // 添加部门弹层不显示
      node: null // 记录当前点击的node节点
    }
  },
  created() { // 钩子函数
    this.getDepartments() // 调用自身方法
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
      // console.log(result)
    },
    addDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node // node是当前的点击的部门，此时这个部门应该记录下来
    },
    editDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node // 赋值操作的节点
      this.$refs.addDept.getDepartDetail(node.id) // 父组件 调用子组件的方法,通过 $refs.addDept找到该组件,调用他的方法,传递数据给他
    }
  }

}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
